# $Id$
# Maintainer: Rémy Oudompheng <remy@archlinux.org>

pkgname=texlive-core
pkgver=2018.48566
_revnr=${pkgver#2018.}
pkgrel=1
pkgdesc="TeX Live core distribution"
license=('GPL')
arch=(any)
depends=('texlive-bin' 'perl')
optdepends=(
  'dialog:      for texconfig'
  'ghostscript: for epstopdf, epspdf and other ConTeXt tools' 
  'java-runtime: for utilities like arara'
  'perl-tk:     for texdoctk'
  'psutils:     to manipulate the output of dvips'
  'python:      for pythontex'
  'python2:     for dviasm'
  'ruby:        for old ConTeXT MkII and epspdf'
  't1utils:     can be useful when installing Type1 fonts'
)
groups=('texlive-most')
conflicts=('tetex' 'texlive-latex3' 'pdfjam'
           'texlive-genericextra'
           'texlive-plainextra')
provides=('tetex' 'texlive-latex3' 'pdfjam'
          'texlive-genericextra'
          'texlive-plainextra')
replaces=('tetex' 'texlive-latex3' 'pdfjam'
          'texlive-genericextra'
          'texlive-plainextra')
url='http://tug.org/texlive/'
source=("https://sources.archlinux.org/other/texlive/$pkgname-$pkgver-src.zip"
        "$pkgname.maps"
        "$pkgname.fmts"
        "mktexlsr.hook"
        "mktexlsr.script"
        "texlive-updmap.hook"
        "texlive-updmap.script"
        "texlive-fmtutil.hook"
        "texlive-fmtutil.script"
        "texmf.cnf"
        "texmfcnf.lua"
        "09-texlive-fonts.conf")
install=texlive.install
backup=(etc/texmf/web2c/texmf.cnf \
        etc/texmf/chktex/chktexrc \
	etc/texmf/dvipdfmx/dvipdfmx.cfg \
	etc/texmf/dvips/config/config.ps \
	etc/texmf/tex/generic/config/language.dat \
	etc/texmf/tex/generic/config/language.def \
	etc/texmf/tex/generic/tex-ini-files/pdftexconfig.tex \
	etc/texmf/ttf2pk/ttf2pk.cfg \
	etc/texmf/web2c/fmtutil.cnf \
	etc/texmf/web2c/mktex.cnf \
	etc/texmf/xdvi/XDvi)
sha256sums=('1eacaf8df21a50e35d589349ed575cd203b1957b33628828788f693fceb3eb9b'
            '275253728d884b2b75e01840b0824c1fe5afbdda91fafeacd33deb6cd92341a3'
            '009ab6be9e4fa18709fc74d26a3777372f4b5af9313421ff0546ba94a9d5ff13'
            '1d62d46d4d3a538ccad48d31192fccdfc8d6c0a60b331575a10a007d46812d46'
            '05afeae62a5d4c9de79c838c9636e2aefe9ad1d6b787fed4e5930c13baf60eba'
            '9a0e462ad44d2ced432860ff62288b1c2f55b04c8d0c92887672478752417cf4'
            'ee6e76192a5ad880a2152cd7900b86c8465239fb228045a2f8360b0d7a449f4a'
            '1f2c67e3cab02398980008f3095257ade3bb79f188cf0815a2671af3080e21af'
            'f96e9f815fa0a4b85e677f2a9215d9106b8abe46eceb3f3e36a6c76eda3e4a85'
            '65ffceec62ccb6e1b01f58ff8e7ec6123f6385139d45bec7f8332f6f3c717912'
            '0b6c3ee516608ce04d7133db52cadfa1be5d885b3f82bb39dc5897b213847e0d'
            '5e79c40cf3ab93348fc89e97890198601767ea2c8fea89ea76088c17a2b35962')

build() {
   cd "$srcdir"
   echo -n "   --> extracting all packages... "
   for p in *.tar.xz; do 
     bsdtar -xf $p
   done
   echo "done"
   rm -rf source doc
}

package() {
   cd "$srcdir"

   # Install packages.
   install -m755 -d $pkgdir/var/lib/texmf/arch/installedpkgs
   sed -i '/^#/d' CONTENTS
   install -m644 CONTENTS $pkgdir/var/lib/texmf/arch/installedpkgs/${pkgname}_${_revnr}.pkgs
   install -m644 $pkgname.maps $pkgdir/var/lib/texmf/arch/installedpkgs/
   install -m644 $pkgname.fmts $pkgdir/var/lib/texmf/arch/installedpkgs/
   install -m755 -d $pkgdir/usr/share
   wanteddirs=$(for d in *; do test -d $d && [[ $d != texmf* ]] && echo $d; done) || true
   for dir in $wanteddirs; do
     find $dir -type d -exec install -d -m755 $pkgdir/usr/share/texmf-dist/'{}' \;
     find $dir -type f -exec install -m644 '{}' $pkgdir/usr/share/texmf-dist/'{}' \;
   done
   find texmf-dist -type d -exec install -d -m755 $pkgdir/usr/share/'{}' \;
   find texmf-dist -type f -exec install -m644 '{}' $pkgdir/usr/share/'{}' \;
   find texmf-dist -type f -executable -exec chmod 755 $pkgdir/usr/share/'{}' \;
 
   #############################################################
   ### install texmf tree
   echo "--> installing the /etc/texmf tree"
   install -d -m755 "$pkgdir"/etc/texmf/web2c
   install -d -m755 "$pkgdir"/etc/texmf/chktex
   install -d -m755 "$pkgdir"/etc/texmf/dvips/config
   install -d -m755 "$pkgdir"/etc/texmf/dvipdfmx
   install -d -m755 "$pkgdir"/etc/texmf/tex/generic/config
   install -d -m755 "$pkgdir"/etc/texmf/tex/generic/tex-ini-files
   install -d -m755 "$pkgdir"/etc/texmf/ttf2pk
   install -d -m755 "$pkgdir"/etc/texmf/xdvi
   install -d -m755 "$pkgdir"/etc/fonts/conf.avail
   install -m644 "$srcdir"/09-texlive-fonts.conf "$pkgdir"/etc/fonts/conf.avail/

   # Remove manpages (already in texlive-bin).
   rm -rf "$pkgdir"/usr/share/texmf-dist/doc/man

   # copy config files to $TEXMFCONFIG tree
   cp -a "$pkgdir"/usr/share/texmf-dist/chktex/chktexrc \
     	"$pkgdir"/etc/texmf/chktex/
   cp -a "$pkgdir"/usr/share/texmf-dist/web2c/mktex.cnf \
   	  "$pkgdir"/etc/texmf/web2c/
   cp -a "$pkgdir"/usr/share/texmf-dist/web2c/updmap-hdr.cfg \
   	  "$pkgdir"/etc/texmf/web2c/
   cp -a "$pkgdir"/usr/share/texmf-dist/web2c/fmtutil-hdr.cnf \
   	  "$pkgdir"/etc/texmf/web2c/fmtutil.cnf
   cp -a "$pkgdir"/usr/share/texmf-dist/dvips/config/config.ps \
   	  "$pkgdir"/etc/texmf/dvips/config/
   cp -a "$pkgdir"/usr/share/texmf-dist/dvipdfmx/dvipdfmx.cfg \
   	  "$pkgdir"/etc/texmf/dvipdfmx/
   cp -a "$pkgdir"/usr/share/texmf-dist/tex/generic/tex-ini-files/pdftexconfig.tex \
   	  "$pkgdir"/etc/texmf/tex/generic/tex-ini-files/
   cp -a "$pkgdir"/usr/share/texmf-dist/tex/generic/config/language.dat \
   	  "$pkgdir"/etc/texmf/tex/generic/config/
   cp -a "$pkgdir"/usr/share/texmf-dist/tex/generic/config/language.def \
   	  "$pkgdir"/etc/texmf/tex/generic/config/
   cp -a "$pkgdir"/usr/share/texmf-dist/ttf2pk/ttf2pk.cfg \
   	  "$pkgdir"/etc/texmf/ttf2pk/
   cp -a "$pkgdir"/usr/share/texmf-dist/xdvi/XDvi \
   	  "$pkgdir"/etc/texmf/xdvi/
   # remove TL specific warnings in the language.{dat,def} files:
   sed -i -e '/DO NOT EDIT/,+3 d' "$pkgdir"/etc/texmf/tex/generic/config/language.*

   # replace upstream texmf.cnf with ours
   rm -f "$pkgdir"/usr/share/texmf-dist/web2c/texmf.cnf
   install -m644 "$srcdir"/texmf.cnf "$pkgdir"/etc/texmf/web2c/texmf.cnf
   # since the location of texmf.cnf is hard-wired to be under /usr/share/texmf/web2c
   # we make a symlink from /etc/texmf/web2c/texmf.cnf to the latter
   ln -sf /etc/texmf/web2c/texmf.cnf "$pkgdir"/usr/share/texmf-dist/web2c/texmf.cnf
   # replace upstream texmfcnf.lua with ours
   install -m644 "$srcdir"/texmfcnf.lua "$pkgdir"/usr/share/texmf-dist/web2c/texmfcnf.lua

   # create symlinks for formats 
   echo "--> Create symlinks for TeX formats ..."
   mkdir -p "${pkgdir}/usr/bin"
   bash "${pkgdir}"/usr/share/texmf-dist/scripts/texlive/texlinks.sh -f "$pkgdir"/usr/share/texmf-dist/web2c/fmtutil.cnf "$pkgdir"/usr/bin/

   # use python2 instead of python for scripts.
   sed -i '1s/python/python2/' $pkgdir/usr/share/texmf-dist/scripts/de-macro/de-macro
   sed -i 's/env python/env python2/' $pkgdir/usr/share/texmf-dist/scripts/dviasm/dviasm.py

   # install Perl libraries
   mv "$pkgdir"/usr/share/texmf-dist/tlpkg "$pkgdir"/usr/share
   rm -rf "$pkgdir"/usr/share/tlpkg/tlpobj

   ## install pacman hooks
   install -D -m644 ${srcdir}/mktexlsr.hook "$pkgdir/usr/share/libalpm/hooks/mktexlsr.hook"
   install -D -m644 ${srcdir}/texlive-updmap.hook "$pkgdir/usr/share/libalpm/hooks/texlive-updmap.hook"
   install -D -m644 ${srcdir}/texlive-fmtutil.hook "$pkgdir/usr/share/libalpm/hooks/texlive-fmtutil.hook"
   install -D -m755 ${srcdir}/mktexlsr.script "$pkgdir/usr/share/libalpm/scripts/mktexlsr"
   install -D -m755 ${srcdir}/texlive-updmap.script "$pkgdir/usr/share/libalpm/scripts/texlive-updmap"
   install -D -m755 ${srcdir}/texlive-fmtutil.script "$pkgdir/usr/share/libalpm/scripts/texlive-fmtutil"

   # copy config file to texmf-config
   #mkdir -p $pkgdir/etc/texmf/tex/context/config
   #cp -a $pkgdir/usr/share/texmf-dist/tex/context/config/cont-usr.tex \
   #	  $pkgdir/etc/texmf/tex/context/config/cont-usr.tex
   
   # remove upstream updmap.cfg: it contains too many maps.
   rm "${pkgdir}/usr/share/texmf-dist/web2c/updmap.cfg"
   # remove upstream fmtutil.cnf: it will be autogenerated
   rm "${pkgdir}/usr/share/texmf-dist/web2c/fmtutil.cnf"
 
   # more cleanup
   rm -rf $pkgdir/usr/share/texmf-dist/scripts/context/stubs/mswin/

    # link programs from /usr/share/texmf-dist/scripts
    _linked_scripts="
a2ping/a2ping.pl
accfonts/mkt1font
accfonts/vpl2ovp
accfonts/vpl2vpl
adhocfilelist/adhocfilelist.sh
arara/arara.sh
bundledoc/arlatex
bundledoc/bundledoc
checkcites/checkcites.lua
checklistings/checklistings.sh
chktex/chkweb.sh
chktex/deweb.pl
cjk-gs-integrate/cjk-gs-integrate.pl
context/perl/mptopdf.pl
context/stubs/unix/context
context/stubs/unix/contextjit
context/stubs/unix/luatools
context/stubs/unix/mtxrun
context/stubs/unix/mtxrunjit
context/stubs/unix/texexec
context/stubs/unix/texmfstart
ctan-o-mat/ctan-o-mat.pl
ctanbib/ctanbib
ctanify/ctanify
ctanupload/ctanupload.pl
de-macro/de-macro
dosepsbin/dosepsbin.pl
dtxgen/dtxgen
dviasm/dviasm.py
dviinfox/dviinfox.pl
epstopdf/epstopdf.pl
findhyph/findhyph
fontools/afm2afm
fontools/autoinst
fontools/ot2kpx
fragmaster/fragmaster.pl
installfont/installfont-tl
jfmutil/jfmutil.pl
ketcindy/ketcindy.sh
latex-git-log/latex-git-log
latex-papersize/latex-papersize.py
latex2man/latex2man
latex2nemeth/latex2nemeth
latexdiff/latexdiff-vc.pl
latexdiff/latexdiff.pl
latexdiff/latexrevise.pl
latexfileversion/latexfileversion
latexindent/latexindent.pl
latexmk/latexmk.pl
latexpand/latexpand
ltxfileinfo/ltxfileinfo
ltximg/ltximg.pl
lua2dox/lua2dox_filter
luaotfload/luaotfload-tool.lua
lwarp/lwarpmk.lua
make4ht/make4ht
match_parens/match_parens
mf2pt1/mf2pt1.pl
mkjobtexmf/mkjobtexmf.pl
oberdiek/pdfatfi.pl
pdfbook2/pdfbook2
pdfcrop/pdfcrop.pl
pdfjam/pdf180
pdfjam/pdf270
pdfjam/pdf90
pdfjam/pdfbook
pdfjam/pdfflip
pdfjam/pdfjam
pdfjam/pdfjam-pocketmod
pdfjam/pdfjam-slides3up
pdfjam/pdfjam-slides6up
pdfjam/pdfjoin
pdfjam/pdfnup
pdfjam/pdfpun
pdflatexpicscale/pdflatexpicscale.pl
pdfxup/pdfxup
pfarrei/a5toa4.tlu
pfarrei/pfarrei.tlu
pkfix-helper/pkfix-helper
pkfix/pkfix.pl
ps2eps/ps2eps.pl
purifyeps/purifyeps
pythontex/pythontex.py
pythontex/depythontex.py
simpdftex/simpdftex
srcredact/srcredact.pl
sty2dtx/sty2dtx.pl
texcount/texcount.pl
tex4ebook/tex4ebook
tex4ht/ht.sh
tex4ht/htcontext.sh
tex4ht/htlatex.sh
tex4ht/htmex.sh
tex4ht/httex.sh
tex4ht/httexi.sh
tex4ht/htxelatex.sh
tex4ht/htxetex.sh
tex4ht/mk4ht.pl
tex4ht/xhlatex.sh
texdef/texdef.pl
texdiff/texdiff
texdirflatten/texdirflatten
texdoc/texdoc.tlu
texdoctk/texdoctk.pl
texfot/texfot.pl
texlive/allcm.sh
texlive/allneeded.sh
texlive/dvi2fax.sh
texlive/dvired.sh
texlive/e2pall.pl
texlive/fmtutil-sys.sh
texlive/fmtutil-user.sh
texlive/fmtutil.pl
texlive/fontinst.sh
texlive/kpsetool.sh
texlive/kpsewhere.sh
texlive/ps2frag.sh
texlive/pslatex.sh
texlive/rungs.tlu
texlive/texconfig-dialog.sh
texlive/texconfig-sys.sh
texlive/texconfig.sh
texlive/texlinks.sh
texlive/updmap-sys.sh
texlive/updmap-user.sh
texlive/updmap.pl
texliveonfly/texliveonfly.py
texloganalyser/texloganalyser
thumbpdf/thumbpdf.pl
typeoutfileinfo/typeoutfileinfo.sh
xindy/texindy.pl
xindy/xindy.pl
"
    install -m755 -d $pkgdir/usr/bin
    for _script in ${_linked_scripts}; do
        _scriptbase=$(basename $_script)
        _scriptbase=${_scriptbase%.*}
        ln -s /usr/share/texmf-dist/scripts/${_script} ${pkgdir}/usr/bin/${_scriptbase}
    done
    ln -s /usr/share/texmf-dist/scripts/listings-ext/listings-ext.sh ${pkgdir}/usr/bin/listings-ext.sh

    # additional symlinks
    ln -s allcm ${pkgdir}/usr/bin/allec
    ln -s fmtutil ${pkgdir}/usr/bin/mktexfmt
    ln -s kpsetool ${pkgdir}/usr/bin/kpsexpand
    ln -s kpsetool ${pkgdir}/usr/bin/kpsepath
    ln -s epstopdf ${pkgdir}/usr/bin/repstopdf
    ln -s pdfcrop ${pkgdir}/usr/bin/rpdfcrop
    ln -s luaotfload-tool ${pkgdir}/usr/bin/mkluatexfontdb
    ln -s texdef ${pkgdir}/usr/bin/latexdef
}
